/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmOptions.java
 *
 * Created on Jul 30, 2009, 5:23:17 PM
 */
package cconverter;

import java.io.IOException;
import org.jdesktop.application.Action;

/**
 *
 * @author SapunBoj
 */
public class FrmOptions extends javax.swing.JFrame {

    javax.swing.JFrame _frm = this;
    Props props = new Props();

    /** Creates new form FrmOptions */
    public FrmOptions() {
        initComponents();
        try {
            props.makeFile();
            props.loadProperties();
        } catch (Exception ex) {
            new FrmInfo(this, true).setInfo("Error", "Error reading options: " + ex.getLocalizedMessage());
        }
        myInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @Action // getOptions and fill OptionsForm
    public void clickCancel() {
        fillSets();
        this.dispose();
    }

    @Action
    public void clickOK() {
        if (chbAOT.isSelected()) {
            props.setProp("isAOT", "true");
        } else {
            props.setProp("isAOT", "false");
        }

        if (chbAU.isSelected()) {
            props.setProp("isAU", "true");
        } else {
            props.setProp("isAU", "false");
        }

        props.setProp("decimalPlaces", spinDP.getValue().toString());

        try {
            props.storeProperties();
        } catch (IOException ex) {
            new FrmInfo(this, true).setInfo("Error", ex.getLocalizedMessage());
        }
        this.dispose();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMain = new javax.swing.JPanel();
        chbAOT = new javax.swing.JCheckBox();
        chbAU = new javax.swing.JCheckBox();
        btnCancel = new javax.swing.JButton();
        btnOK = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        lblDecimalPlaces = new javax.swing.JLabel();
        spinDP = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getResourceMap(FrmOptions.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        pnlMain.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("pnlMain.border.title"))); // NOI18N
        pnlMain.setName("pnlMain"); // NOI18N

        chbAOT.setText(resourceMap.getString("chbAOT.text")); // NOI18N
        chbAOT.setName("chbAOT"); // NOI18N

        chbAU.setText(resourceMap.getString("chbAU.text")); // NOI18N
        chbAU.setName("chbAU"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getActionMap(FrmOptions.class, this);
        btnCancel.setAction(actionMap.get("clickCancel")); // NOI18N
        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N

        btnOK.setAction(actionMap.get("clickOK")); // NOI18N
        btnOK.setText(resourceMap.getString("btnOK.text")); // NOI18N
        btnOK.setName("btnOK"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        lblDecimalPlaces.setText(resourceMap.getString("lblDecimalPlaces.text")); // NOI18N
        lblDecimalPlaces.setName("lblDecimalPlaces"); // NOI18N

        spinDP.setName("spinDP"); // NOI18N
        spinDP.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinDPStateChanged(evt);
            }
        });

        javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
        pnlMain.setLayout(pnlMainLayout);
        pnlMainLayout.setHorizontalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                    .addComponent(chbAOT)
                    .addComponent(chbAU)
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addComponent(lblDecimalPlaces)
                        .addGap(18, 18, 18)
                        .addComponent(spinDP, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlMainLayout.createSequentialGroup()
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnlMainLayout.setVerticalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chbAOT)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chbAU)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDecimalPlaces)
                    .addComponent(spinDP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnOK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void spinDPStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinDPStateChanged
        // TODO add your handling code here:
        int value = Integer.parseInt(spinDP.getValue().toString());
        if (value < 1) {
            spinDP.setValue(1);
        } else if (value > 8) {
            spinDP.setValue(8);
        }
    }//GEN-LAST:event_spinDPStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JCheckBox chbAOT;
    private javax.swing.JCheckBox chbAU;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblDecimalPlaces;
    private javax.swing.JPanel pnlMain;
    private javax.swing.JSpinner spinDP;
    // End of variables declaration//GEN-END:variables

    private void myInit() {
        this.setResizable(false);
        this.setIconImage(Sets.icon);
        this.setLocationRelativeTo(null);
        fillSets();
        this.pack();
    }

    public void fillSets() {
        //get properties
        if (props.getProp("isAOT", "false").equals("false")) {
            chbAOT.setSelected(false);
        } else if (props.getProp("isAOT", "false").equals("true")) {
            chbAOT.setSelected(true);
        }

        if (props.getProp("isAU", "false").equals("false")) {
            chbAU.setSelected(false);
        } else if (props.getProp("isAU", "false").equals("true")) {
            chbAU.setSelected(true);
        }
        Object setvalue = (Object) Integer.parseInt(props.getProp("decimalPlaces", "2"));
        spinDP.setValue(setvalue);
    }
}
